package com.learn.bus;

import com.learn.bus.data.MessageData;
import com.learn.bus.data.StatusData;
import com.learn.bus.member.MessageCollectMember;
import com.learn.bus.member.StatusMember;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;


/**
 * <p>项目名称: design-pattern </p>
 *
 * <p>文件名称: App </p>
 *
 * <p>The Data-Bus pattern provides a method where different parts of an application may
 * pass messages between each other without needing to be aware of the other's existence.</p>
 *
 * <p>Similar to the {@code ObserverPattern}, members register themselves with the {@link DataBus}
 * and may then receive each piece of data that is published to the Data-Bus. The member
 * may react to any given message or not.</p>
 *
 * <p>It allows for Many-to-Many distribution of data, as there may be any number of
 * publishers to a Data-Bus, and any number of members receiving the data. All members
 * will receive the same data, the order each receives a given piece of data, is an
 * implementation detail.</p>
 *
 * <p>Members may unsubscribe from the Data-Bus to stop receiving data.</p>
 *
 * <p>This example of the pattern implements a Synchronous Data-Bus, meaning that
 * when data is published to the Data-Bus, the publish method will not return until
 * all members have received the data and returned.</p>
 *
 * <p>The {@link DataBus} class is a Singleton.</p>
 * <p>Members of the Data-Bus must implement the {@link Member} interface.</p>
 * <p>Data to be published via the Data-Bus must implement the {@link Data} interface.</p>
 * <p>The {@code data} package contains example {@link Data} implementations.</p>
 * <p>The {@code members} package contains example {@link Member} implementations.</p>
 * <p>The {@link StatusMember} demonstrates using the DataBus to publish a message
 * to the Data-Bus when it receives a message.</p>
 */
@Slf4j
public class App {

    public static void main(String[] args) {
        final DataBus dataBus = DataBus.getInstance();
        StatusMember s1 = new StatusMember();
        StatusMember s2 = new StatusMember();

        dataBus.subscribe(s1);
        dataBus.subscribe(s2);

        final MessageCollectMember foo = new MessageCollectMember("Foo");
        final MessageCollectMember bar = new MessageCollectMember("Bar");

        dataBus.subscribe(foo);

        StatusData stateData = new StatusData(LocalDateTime.now(), "开始");
        dataBus.publish(stateData);

        MessageData m1 = new MessageData("上车");
        dataBus.publish(m1);

        log.info("-----------------------------");

        dataBus.subscribe(bar);
        MessageData m2 = new MessageData("下车");
        dataBus.publish(m2);

        StatusData endData = new StatusData(LocalDateTime.now(), "结束");
        dataBus.publish(endData);

    }


}
